package com.hx.springboot1.service.impl;

import com.hx.springboot1.dao.*;
import com.hx.springboot1.pojo.dto.ChooseDTO;
import com.hx.springboot1.pojo.dto.ScoreModelDTO;
import com.hx.springboot1.pojo.entity.*;
import com.hx.springboot1.pojo.vo.ScoreModelVO;
import com.hx.springboot1.service.ChooseService;
import com.hx.springboot1.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: hx
 * Date: 2021-05-25 23/32
 * Description: Nothing else
 */
@Service
public class ChooseServiceImpl implements ChooseService {
    @Autowired
    ScoreDao scoreDao;
    @Autowired
    YearDao yearDao;
    @Autowired
    UserDao userDao;
    @Autowired
    StuDao stuDao;
    @Autowired
    ChooseDao chooseDao;
    @Override
    public ScoreModelVO selectByStuIdFromChoose(Integer stuId) {
        ScoreModelVO scoreModelVO = new ScoreModelVO();
        if (stuId==null){
            scoreModelVO.setMessage("请输入学号！");
            scoreModelVO.setState(false);
            return scoreModelVO;
        }
        TabStudent tabStudent = stuDao.selectByStuId(stuId);
        if (tabStudent==null){
            scoreModelVO.setMessage("库中没有该学生！请检查学号");
            scoreModelVO.setState(false);
            return scoreModelVO;
        }
        List<ScoreModelDTO> scoreModelDTOS = chooseDao.selectStuAndCourseByStuId(stuId);
        if (scoreModelDTOS.size()>0&&scoreModelDTOS!=null){
            scoreModelVO.setInfo(scoreModelDTOS);
            scoreModelVO.setMessage("查到学生和课程了");
            scoreModelVO.setState(true);
            return scoreModelVO;
        }else {
            scoreModelVO.setInfo(null);
            scoreModelVO.setMessage("该学生所选课程的成绩已经全部录入完毕！");
            scoreModelVO.setState(false);
            return scoreModelVO;
        }
    }

    @Override
    public List<ChooseDTO> getChooseByStuId(String username) {
        TabUser login = userDao.login(username);
        int userId = login.getUserId();
        TabStudent tabStudent;
        tabStudent = stuDao.selectByUserId(userId);
        if (tabStudent!=null){
            /* 说明该用户 为 学生 */
            List<ChooseDTO> chooseDTOS;
            chooseDTOS = chooseDao.getChooseByStuId(tabStudent.getStuId());
            if (chooseDTOS!=null&&chooseDTOS.size()>0){
                return chooseDTOS;
            }
            List<ChooseDTO> infoList = new ArrayList<>();
            ChooseDTO chooseDTO = new ChooseDTO();
            chooseDTO.setStuId(0);
            chooseDTO.setChooseId(0);
            chooseDTO.setCourseId(0);
            chooseDTO.setCourseName("您还未选课，");
            chooseDTO.setYearId(0);
            chooseDTO.setYearName("请先选课！");
            chooseDTO.setChooseTerm(0);
            infoList.add(chooseDTO);
            return infoList;
        }else {
            return null;
        }
    }

    @Override
    public List<TabYear> getYear() {
        List<TabYear> year = chooseDao.getYear();
        return year;
    }

    @Override
    public ResultMsg saveChoose(Integer yearId, Integer courseId) {
        int term;
        int effectRows = 0;
        int stuId = 0;
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<TabUser> tabUsers = userDao.selectAllUserByName(userDetails.getUsername());
        if (tabUsers!=null&&tabUsers.size()>0){
            int userId = tabUsers.get(0).getUserId();
            TabStudent tabStudent = stuDao.selectByUserId(userId);
            if (tabStudent!=null){
                stuId = tabStudent.getStuId();
            }
        }
        if (stuId==0||yearId==null||courseId==null){
            return new ResultMsg("请填写数据后再保存！",false);
        }
        if (yearId%2==0){
            term = 2;
        }else {
            term = 1;
        }
        List<TabChoose> tabChooses;
        tabChooses = chooseDao.checkChoose(stuId, courseId,yearId);
        TabYear tabYear = yearDao.selectYearById(yearId);
        String yearName = tabYear.getYearName();
        if (tabChooses!=null&&tabChooses.size()>0){
            /* 说明已经选过了这个课 */
            return new ResultMsg("在["+yearName+"]学年 您已经选过这门课了！",false);
        }else {
            effectRows += chooseDao.insertChoose(stuId, courseId, yearId, term);
            if (effectRows>0){
                return new ResultMsg("选课成功！",true);
            }else {
                return new ResultMsg("选课失败！",false);
            }
        }
    }

    @Override
    public ResultMsg delChoose(Integer chooseId) {
        int effectRows = 0;
        if (chooseId==null){
            return new ResultMsg("当前选课ID获取失败！",false);
        }
        TabScore tabScore;
        TabChoose tabChoose = chooseDao.selectByChooseId(chooseId);
        tabScore = scoreDao.selectByStuIdAndCourseId(tabChoose.getStuId(), tabChoose.getCourseId());
        if (tabScore!=null){
            return new ResultMsg("成绩已录入系统，无法取消选课！",false);
        }else {
            effectRows += chooseDao.delChoose(chooseId);
            if (effectRows>0){
                return new ResultMsg("取消选课成功！",true);
            }else {
                return new ResultMsg("取消失败！请重试！",false);
            }
        }
    }
}
